#include <iostream>

using std::cout;
using std::endl;

//i^2 = -1

class Complex
{
public:
    Complex(double dreal = 0.0, double dimag = 0.0)
    : _dreal(dreal)
    , _dimag(dimag)
    {
        cout << "Complex(double = 0.0, double = 0.0)" << endl;
    }

    //对于对象本身会发生变化的运算符，建议以成员函数的形式
    //进行重载
    //com2 += com1
    Complex &operator+=(const Complex &rhs)
    {
        cout << "Complex &operator+=(const Complex &)" << endl;
        _dreal += rhs._dreal;
        _dimag += rhs._dimag;

        return *this;
    }

    //前置++
    Complex &operator++()
    {
        cout << "Complex &operator++()" << endl;
        ++_dreal;
        ++_dimag;

        return *this;
    }

    //后置++
    Complex operator++(int)//此处的int仅仅起到标识作用
    {
        cout << "Complex operator++(int)" << endl;
        Complex tmp(*this);//留下++之前的结果
        ++_dreal;
        ++_dimag;
        return tmp;
    }

    void print() const
    {
        cout << _dreal << " + " << _dimag << "i" << endl;
    }

    double getReal() const
    {
        return _dreal;
    }

    double getImag() const
    {
        return _dimag;
    }

    ~Complex()
    {
        cout << "~Complex()" << endl;
    }
    friend Complex operator+(const Complex &lhs, const Complex &rhs);
private:
    double _dreal;
    double _dimag;
};

//运算符重载之友元函数
Complex operator+(const Complex &lhs, const Complex &rhs)
{
    cout << "friend Complex operator+(const, const)" << endl;
    return Complex(lhs._dreal + rhs._dreal, 
                   lhs._dimag + rhs._dimag);
}

void test()
{
    int a = 10;
    cout <<"++a = " << ++a << ",a = " << a <<endl;//11 11
    cout <<"a++ = " << a++ << ",a = " << a << endl;//11 12

    Complex com1(1, 2);
    cout << "com1 = ";
    com1.print();

    cout << endl;
    cout << "++com1 = ";
    (++com1).print();//(2, 3)
    cout << "com1 = ";
    com1.print();//(2, 3)

    cout << endl;
    cout << "com1++ = ";
    (com1++).print();//(2, 3)
    cout << "com1 = ";
    com1.print();//(3, 4)
                 //
    /* &(++com1);//ok */
    /* &(com1++);//error */
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

